/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.prefs.Preferences;
import javax.persistence.EntityManager;

/**
 *
 * @author anhvh_b00319
 */
public class DataAccess {

    private static String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static String connectionString = "jdbc:sqlserver://localhost:1433;databaseName=PrimePhysique";
    private static String username = "ppsa";
    private static String password = "fat123456";
    private static Preferences pref = Preferences.userNodeForPackage(DAL.DataAccess.class);
    private static Connection con;

    public static String getConnectionString() {
        return connectionString;
    }

    public static void setConnectionString(String connectionString) {
        pref.put("connectionString", connectionString);
        DataAccess.connectionString = connectionString;
    }

    public static String getDriverClass() {
        return driverClass;
    }

    public static void setDriverClass(String driverClass) {
        DataAccess.driverClass = driverClass;
    }

    public static String getPassword() {
        return password;
    }

    public static void setPassword(String password) {
        pref.put("password", password);
        DataAccess.password = password;
    }

    public static String getUsername() {
        return username;
    }

    public static void setUsername(String username) {
        pref.put("username", username);
        DataAccess.username = username;
    }

    public DataAccess() {
    }

    public static boolean testDefaultConnection() throws ClassNotFoundException, SQLException, Exception {

        EntityManager entityManager = null;
        connectionString = pref.get("connectionString", connectionString);
        username = pref.get("username", username);
        password = pref.get("password", password);
        try {
            if (con != null) {
                con.close();
            }
            Class.forName(driverClass);
            con = DriverManager.getConnection(connectionString, username, password);
            entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("PrimePhysiquePU", getConnectionProperties()).createEntityManager();
            entityManager.createNamedQuery("Branches.findAll").getResultList();
            entityManager.createNamedQuery("Client.findAll").getResultList();
            entityManager.createNamedQuery("Enrollment.findAll").getResultList();
            entityManager.createNamedQuery("NewsEvents.findAll").getResultList();
            entityManager.createNamedQuery("Nutrition.findAll").getResultList();
            entityManager.createNamedQuery("Program.findAll").getResultList();
            entityManager.createNamedQuery("Staff.findAll").getResultList();
            entityManager.createNamedQuery("SuccessStory.findAll").getResultList();
            return true;
        } catch (SQLException ex) {
            throw ex;
        } catch (ClassNotFoundException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            if (entityManager != null) {
                entityManager.close();
            }
        }
    }

    public static void testConnection(String url, String user, String pass) throws ClassNotFoundException, SQLException, Exception {
        Connection conn = null;
        EntityManager entityManager = null;
        try {
            Class.forName(driverClass);
            conn = DriverManager.getConnection(url, user, pass);
            Map properties = new HashMap();
            properties.put("javax.persistence.jdbc.url", url);
            properties.put("javax.persistence.jdbc.password", pass);
            properties.put("javax.persistence.jdbc.driver", driverClass);
            properties.put("javax.persistence.jdbc.user", user);
            entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("PrimePhysiquePU", properties).createEntityManager();
            entityManager.createNamedQuery("Branches.findAll").getResultList();
            entityManager.createNamedQuery("Client.findAll").getResultList();
            entityManager.createNamedQuery("Enrollment.findAll").getResultList();
            entityManager.createNamedQuery("NewsEvents.findAll").getResultList();
            entityManager.createNamedQuery("Nutrition.findAll").getResultList();
            entityManager.createNamedQuery("Program.findAll").getResultList();
            entityManager.createNamedQuery("Staff.findAll").getResultList();
            entityManager.createNamedQuery("SuccessStory.findAll").getResultList();
        } catch (ClassNotFoundException ex) {
            throw ex;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            if (entityManager != null) {
                entityManager.close();
            }
        }
    }

    public static Map getConnectionProperties() {
        Map properties = new HashMap();
        properties.put("javax.persistence.jdbc.url", connectionString);
        properties.put("javax.persistence.jdbc.password", password);
        properties.put("javax.persistence.jdbc.driver", driverClass);
        properties.put("javax.persistence.jdbc.user", username);
        return properties;
    }
}
